﻿// Necroz project team © <2015> <ArchLord2EMU>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Author:Sagara
// Date:2015-09-06-23:29

using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace ArchLord2Launcher
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        protected string Argument;

        public MainWindow()
        {
            InitializeComponent();

            if (!string.IsNullOrEmpty(Properties.Settings.Default.Login))
            {
                checkBox.IsChecked = true;

                LoginBox.Text = Properties.Settings.Default.Login;
                PasswordBox.Password = Properties.Settings.Default.Password;
            }

            checkBox.Checked += delegate
            {
                Properties.Settings.Default.Reload();
                Properties.Settings.Default.Login = LoginBox.Text;
                Properties.Settings.Default.Password = PasswordBox.Password;
                Properties.Settings.Default.Save();
            };

            checkBox.Unchecked += delegate
            {
                Properties.Settings.Default.Reset();
                Properties.Settings.Default.Save();
            };
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            const string key = "wzlAUnchEr";

            if (string.IsNullOrEmpty(LoginBox.Text) || string.IsNullOrEmpty(PasswordBox.Password))
            {
                MessageBox.Show("Login or password has not set");

                return;
            }

            try
            {
                if(LoginBox.Text.Length > 18)
                    return;

                string padder = "/";

                if (LoginBox.Text.Length < 18)
                {
                    int s = 18 - LoginBox.Text.Length;

                    for (int i = 0; i < s; i++)
                        LoginBox.Text += padder;                  
                }

                Argument = $"{LoginBox.Text}|{"43674304"}|{CalculateMd5Hash(PasswordBox.Password)}|0|ENG|127.0.0.1-7777";
                Argument = XoR(Argument, key);

                Process.Start("Archlord2.exe", Base64Encode(Argument));
            }
            finally
            {
                Close();
            }
        }

        private string XoR(string text, string key)
        {
            var result = new StringBuilder();

            for (int c = 0; c < text.Length; c++)
                result.Append((char)(text[c] ^ (uint)key[c % key.Length]));

            return result.ToString();
        }

        private string Base64Encode(string plainText)
        {
            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            return Convert.ToBase64String(plainTextBytes);
        }

        private string Base64Decode(string plainText)
        {
            var plainTextBytes = Convert.FromBase64String(plainText);
            return Encoding.UTF8.GetString(plainTextBytes);
        }

        public static string CalculateMd5Hash(string input)
        {
            var md5Hasher = MD5.Create();
            var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            var sBuilder = new StringBuilder();

            foreach (byte t in data)
                sBuilder.Append(t.ToString("x2"));
            
            return sBuilder.ToString();
        }           
    }
}
